/*MyPhotoIndex Free Photo Organizer <www.myphotoindex.com>
  Copyright (C) <2006-2009>  <Asaf Yarkoni asaf@yarkoni.net>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using MyPhotoIndex.Interfaces;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
using System.Threading;
using MyPhotoIndex.Interfaces.Imaging;

namespace MyPhotoIndex.Utilities
{
    public class DesktopWallpaperSetter
    {
        public enum Style : int
        {
            Tiled = 0,
            Centered,
            Stretched
        }

        private Style m_style;

        public void SetWallpaper(List<IImageData> selectedImages, Style style)
        {
            m_style = style;

            ThreadPool.QueueUserWorkItem(new WaitCallback(SetWallpaper), selectedImages);
        }

        private void SetWallpaper(Object state)
        {
            try
            {
                List<IImageData> selectedImages = state as List<IImageData>;

                IImageData imageData = selectedImages[0];
                String wallpaperImagePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "MPI_Wallpaper.bmp");

                Size desktopSize = Screen.PrimaryScreen.WorkingArea.Size;

                using (Image origImage = imageData.FormatProvider.LoadImage(imageData, desktopSize))
                {
                    Size drawingSize = selectedImages.Count == 1 ? origImage.Size : desktopSize;

                    using (Image image = new Bitmap(origImage.Width, origImage.Height))
                    {
                        using (Graphics g = Graphics.FromImage(image))
                        {
                            using (Brush brush = new SolidBrush(SystemColors.Desktop))
                            {
                                g.FillRectangle(brush, 0, 0, origImage.Width, origImage.Height);
                            }

                            g.DrawImageUnscaled(origImage, 0, 0);
                        }

                        image.Save(wallpaperImagePath, ImageFormat.Bmp);
                    }
                }

                SetWallpaper(wallpaperImagePath, m_style);
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
            }
        }

        private void SetWallpaper(String fileName, Style style)
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);

            switch (style)
            {
                case Style.Tiled:
                    key.SetValue(@"WallpaperStyle", 1.ToString());
                    key.SetValue(@"TileWallpaper", 1.ToString());
                    break;
                case Style.Centered:
                    key.SetValue(@"WallpaperStyle", 1.ToString());
                    key.SetValue(@"TileWallpaper", 0.ToString());
                    break;
                case Style.Stretched:
                    key.SetValue(@"WallpaperStyle", 2.ToString());
                    key.SetValue(@"TileWallpaper", 0.ToString());
                    break;
            }

            Win32Interop.SystemParametersInfo(Win32Interop.SPI_SETDESKWALLPAPER,
                0, fileName, Win32Interop.SPIF_UPDATEINIFILE | Win32Interop.SPIF_SENDWININICHANGE);
        }
    }
}
